<?php
/**
 *
 * Meta-Generator Validierung
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


class Yapary_Form_Formular_Meta_Element
{
	private $oSimpleXmlElement		= NULL;


	/**
	 * */
	function __construct ($_oSimpleXmlElement) {
		$this->oSimpleXmlElement = $_oSimpleXmlElement;
	}


	private function getOption ($arrIgnore = array()) {
		$arrPossibilities = array ('id', 'value', 'class', 'style', 'placeholder', 'readonly', 'title', 'required', 'min', 'max');
		$result = '';

		foreach ($arrPossibilities as $key => $val) {
			if (!in_array($val, $arrIgnore)) {
				$result .= (isset($this->oSimpleXmlElement->component->$val)) ? ' '.$val.'="'.utf8_decode($this->oSimpleXmlElement->component->$val).'"' : '';
			}
		}

		return $result;
	}


	private function getObjectData ($_oSimpleXmlElement) {
		$oObject = NULL;

		preg_match('/([a-z0-9_]*)\(([a-z0-9\->_\'\"\,\$ ]*)\)\-\>([a-z0-9_]*)\(([a-z0-9_\'\"\,\(\) ]*)\);?/ims', $_oSimpleXmlElement, $arrResult);

		if (!(count($arrResult) == 5 || count($arrResult) == 3)) {
			return NULL;
		}

		if (count($arrResult) >= 3) {
			$class		= $arrResult[1];
			$classParam = $arrResult[2];

			$classParam = str_replace("'", "", $classParam);
			$classParam = str_replace('"', '', $classParam);

			$oObject = new $class($classParam);
		}
		if (count($arrResult) == 5) {
			$method		= $arrResult[3];
			$methodParam= $arrResult[4];

			$methodParam = str_replace("'", "", $methodParam);
			$methodParam = str_replace('"', '', $methodParam);

			$oObject = new ReflectionMethod($class, $method);
			return $oObject->invokeArgs(new $class($classParam), explode(',', $methodParam));
		} else {
			return $oObject;
		}
	}


	private function getSelect () {
		$result = '';

		if (!isset($this->oSimpleXmlElement->component->choice)) {
			throw new Exception('Unknown property "choice"');
		}
		if (!isset($this->oSimpleXmlElement->component->name)) {
			throw new Exception('Unknown property "name"');
		}

		$object	= (string) $this->oSimpleXmlElement->component->choice->object;
		$arr = $this->getObjectData($object);

		$option = $this->getOption(array('value'));
		if (count($arr)) {
			foreach ($arr as $key => $val) {
				$result .= '<option value="'.$key.'">'.$val.'</option>';
			}

			return $this->getBorder('<select id="'.$this->oSimpleXmlElement->component->name.'" name="'.$this->oSimpleXmlElement->component->name.'" '.$option.'>'.$result.'</select>');
		}

		return NULL;
	}


	private function getRadio () {
		$result = '';

		if (!isset($this->oSimpleXmlElement->component->choice)) {
			throw new Exception('Unknown property "choice"');
		}
		if (!isset($this->oSimpleXmlElement->component->name)) {
			throw new Exception('Unknown property "name"');
		}

		$object	= (string) $this->oSimpleXmlElement->component->choice->object;
		$arr = $this->getObjectData($object);

		$option = $this->getOption(array('value'));
		$img = (isset($this->oSimpleXmlElement->img->src)) ? '<img src="'.$this->oSimpleXmlElement->img->src.'" />' : '';

		if (count($arr)) {
			foreach ($arr as $key => $arrVal) {
				$val	= $arrVal['Id'];
				$title	= $arrVal['Desc'];
				$text	= $arrVal['Name'];
				$result .= '<div title="'.$title.'" class="orderKind v24_round_edge"><input type="radio" name="'.$this->oSimpleXmlElement->component->name.'" value="'.$text.'" '.$option.' /><label>'.$text.'</label>'.str_replace('{{PLACEHOLDER}}', $val, $img).'</div>';
			}

			return $this->getBorder($result);
		}

		return NULL;
	}


	private function getCheckbox () {
		$result = '';

		if (!isset($this->oSimpleXmlElement->component->choice)) {
			throw new Exception('Unknown property "choice"');
		}
		if (!isset($this->oSimpleXmlElement->component->name)) {
			throw new Exception('Unknown property "name"');
		}

		$object	= (string) $this->oSimpleXmlElement->component->choice->object;
		$arr = $this->getObjectData($object);

		$option = $this->getOption(array('value'));
		$img = (isset($this->oSimpleXmlElement->img->src)) ? '<img src="'.$this->oSimpleXmlElement->img->src.'" />' : '';

		if (count($arr)) {
			$name = (string) $this->oSimpleXmlElement->component->name;
			foreach ($arr as $key => $val) {
				$checked = ' ';
				if (isset($_POST[$name]) && in_array($key, $_POST[$name])) {
					$checked = ' checked="checked"';
				}
				$result .= '<li style="list-style-type: none; margin: 5px;"><input type="checkbox"'.$checked.' name="'.$this->oSimpleXmlElement->component->name.'" value="'.$key.'" /> '.$val.str_replace('{{PLACEHOLDER}}', $key, $img).'</li>';
			}

			return $this->getBorder('<ul '.$option.'>'.$result.'</ul>');
		}

		return NULL;
	}


	private function getTextbox () {
		$result = '';

		if (!isset($this->oSimpleXmlElement->component->name)) {
			throw new Exception('Unknown property "name"');
		}
		if (!isset($this->oSimpleXmlElement->component->type)) {
			throw new Exception('Unknown property "type"');
		}

		$object	= (string) $this->oSimpleXmlElement->component->choice->object;
		$arr = $this->getObjectData($object);

		$option = $this->getOption();
		return $this->getBorder('<input type="'.$this->oSimpleXmlElement->component->type.'" id="'.$this->oSimpleXmlElement->component->name.'" name="'.$this->oSimpleXmlElement->component->name.'" '.$option.'>');
	}


	private function getTextarea () {
		$result = '';

		if (!isset($this->oSimpleXmlElement->component->name)) {
			throw new Exception('Unknown property "name"');
		}
		if (!isset($this->oSimpleXmlElement->component->type)) {
			throw new Exception('Unknown property "type"');
		}

		$object	= (string) $this->oSimpleXmlElement->component->choice->object;
		$arr = $this->getObjectData($object);

		$option = $this->getOption();
		return $this->getBorder('<textarea name="'.$this->oSimpleXmlElement->component->name.'" '.$option.'></textarea>');
	}


	private function getBorder ($_element) {
		$addon	= '';
		$addonClass = '';
		$title	= '';

		if (isset($this->oSimpleXmlElement->addon->pre->class)) {
			$addon = '<span class="add-on"><i class="'.$this->oSimpleXmlElement->addon->pre->class.'"></i></span>';
			$addonClass = 'input-prepend';
		} else if (isset($this->oSimpleXmlElement->addon->pre->text)) {
			$addon = '<span class="add-on">'.$this->oSimpleXmlElement->addon->pre->text.'</span>';
			$addonClass = 'input-prepend';
		}

		if (isset($this->oSimpleXmlElement->desc)) {
			$title = '<div class="form-inline"><label>'.utf8_decode($this->oSimpleXmlElement->desc).'</label></div>';
		}

		return '<div class="'.$addonClass.'">
    				<div class="form-horizontal">
	    				'.$title.'
	    				<div class="form-inline">
							'.$addon.'
				    		'.$_element.'
	    				</div>
    				</div>
				</div>
	';
	}

	public function getComponent () {

		if (!isset($this->oSimpleXmlElement->component->type)) {
			throw new Exception('Unknown property "type"');
		}

		switch ($this->oSimpleXmlElement->component->type) {
			case 'select':
				return $this->getSelect();
				break;
			case 'radio':
				return $this->getRadio();
				break;
			case 'checkbox':
				return $this->getCheckbox();
				break;
			case 'text':
			case 'password':
			case 'time':
			case 'date':
			case 'number':
			case 'email':
			case 'tel':
			case 'submit':
				return $this->getTextbox();
				break;
			case 'textarea':
				return $this->getTextarea();
			default:
				$el = $this->oSimpleXmlElement->component->type;
				throw new Exception('Undefined element "'.$el.'"');
		}

		return NULL;
	}


}


?>